package com.poem.springboot_rear.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poem.springboot_rear.common.LogAs;
import com.poem.springboot_rear.common.LogType;
import com.poem.springboot_rear.common.Result;
import com.poem.springboot_rear.entity.UserOrder;
import com.poem.springboot_rear.service.UserOrderService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：
 * 作者： 小祝小祝
 * 日期： 2023/11/20 17:41
 */
@CrossOrigin
@RestController
@RequestMapping("/userOrder")
public class UserOrderController {

    @Resource
    UserOrderService userOrderService;

    /**
     * 新增用户订单
     *
     * @param userOrder
     * @return
     */
    @LogAs(operation = "新增订单", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody UserOrder userOrder) {
        try {
            userOrderService.save(userOrder);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改订单
     *
     * @param userOrder
     * @return
     */
    @LogAs(operation = "修改订单", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody UserOrder userOrder) {
        try {
            userOrderService.updateById(userOrder);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除单个订单
     *
     * @param id
     * @return
     */
    @LogAs(operation = "删除订单（单个", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        userOrderService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除订单
     *
     * @param ids
     * @return
     */
    @LogAs(operation = "删除订单（多个）", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<String> ids) {
        userOrderService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 查询订单信息（单个）
     *
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    public Result select(@PathVariable Integer id) {
        userOrderService.getById(id);
        return Result.success();
    }

    /**
     * 分页查询订单信息
     * @param pageIndex
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageIndex,
                               @RequestParam Integer pageSize,
                               @RequestParam String userId){
        // 创建条件构造器
        QueryWrapper<UserOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(userId),"user_id",userId);
        Page<UserOrder> page = userOrderService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        return Result.success(page);
    }
}
